﻿// CPrintSetDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "CK103.h"
#include "CPrintSetDlg.h"
#include "afxdialogex.h"
#include "./FileSys/CImgOperator.h"
#include "define.h"

const double dMarginMax = 5.0;
const double dMarginMin = 0.5;

const int nDotLeftNum = 1;
const int nDotRightNum = 1;

const int nRowMax = 10;
const int nRowMin = 1;

const int nColMax = 10;
const int nColMin = 1;


void Drawf(CDC *pDC, CPrintInfo *pInfo, void *pVoid)
{
	((CPrintSetDlg*)pVoid)->OnPrint(pDC, pInfo);
}

// CPrintSetDlg 对话框

IMPLEMENT_DYNAMIC(CPrintSetDlg, CMyDialog)

CPrintSetDlg::CPrintSetDlg(CWnd* pParent /*=nullptr*/)
	: CMyDialog(IDD_PRINT_SET_DLG, pParent)
	, m_nPrintRow(0)
	, m_nPrintCol(0)
	, m_nStartPage(0)
	, m_strHeader(_T(""))
	, m_strFooter(_T(""))
	, m_nPrint(-1)
{

}

CPrintSetDlg::~CPrintSetDlg()
{
}

void CPrintSetDlg::DoDataExchange(CDataExchange* pDX)
{
	CMyDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_PRINT_ROW, m_nPrintRow);
	DDV_MinMaxInt(pDX, m_nPrintRow, 1, 10);
	DDX_Text(pDX, IDC_EDIT_PRINT_COL, m_nPrintCol);
	DDV_MinMaxInt(pDX, m_nPrintCol, 1, 10);
	DDX_Control(pDX, IDC_EDIT_MARGIN_TOP, m_editMarginTop);
	DDX_Control(pDX, IDC_EDIT_MARGIN_LEFT, m_editMarginLeft);
	DDX_Control(pDX, IDC_EDIT_MARGIN_BOTTOM, m_editMarginBottom);
	DDX_Control(pDX, IDC_EDIT_MARGIN_RIGHT, m_editMarginRight);
	DDX_Control(pDX, IDC_SPIN_PRINT_ROW, m_spinRow);
	DDX_Control(pDX, IDC_SPIN_PRINT_COL, m_spinCol);
	DDX_Control(pDX, IDC_CHECK_PRINT_PAGE, m_btnPrintPage);
	DDX_Text(pDX, IDC_EDIT_START_PAGE, m_nStartPage);
	DDV_MinMaxInt(pDX, m_nStartPage, 1, 100);
	DDX_Control(pDX, IDC_CHECK_PRINT_HEADER, m_btnPrintHeader);
	DDX_Control(pDX, IDC_CHECK_PRINT_FOOTER, m_btnPrintFooter);
	DDX_Text(pDX, IDC_EDIT_HEADER, m_strHeader);
	DDV_MaxChars(pDX, m_strHeader, 45);
	DDX_Text(pDX, IDC_EDIT_FOOTER, m_strFooter);
	DDV_MaxChars(pDX, m_strFooter, 45);
	DDX_Control(pDX, IDC_BUTTON_PRINT_VIEW, m_btnPrintView);
	DDX_Control(pDX, IDC_BUTTON_PRINT, m_btnPrint);
	DDX_Control(pDX, IDC_BUTTON_PRINT_DEFAULT, m_btnDefault);
}


BEGIN_MESSAGE_MAP(CPrintSetDlg, CMyDialog)
	ON_BN_CLICKED(IDC_CHECK_PRINT_PAGE, &CPrintSetDlg::OnBnClickedCheckPrintPage)
	ON_BN_CLICKED(IDC_CHECK_PRINT_HEADER, &CPrintSetDlg::OnBnClickedCheckPrintHeader)
	ON_BN_CLICKED(IDC_CHECK_PRINT_FOOTER, &CPrintSetDlg::OnBnClickedCheckPrintFooter)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_MARGIN_TOP, &CPrintSetDlg::OnDeltaposSpinMarginTop)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_MARGIN_BOTTOM, &CPrintSetDlg::OnDeltaposSpinMarginBottom)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_MARGIN_LEFT, &CPrintSetDlg::OnDeltaposSpinMarginLeft)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_MARGIN_RIGHT, &CPrintSetDlg::OnDeltaposSpinMarginRight)
	ON_BN_CLICKED(IDOK, &CPrintSetDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CPrintSetDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON_PRINT_VIEW, &CPrintSetDlg::OnBnClickedButtonPrintView)
	ON_BN_CLICKED(IDC_BUTTON_PRINT, &CPrintSetDlg::OnBnClickedButtonPrint)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_PRINT_DEFAULT, &CPrintSetDlg::OnBnClickedButtonPrintDefault)
END_MESSAGE_MAP()


// CPrintSetDlg 消息处理程序

void CPrintSetDlg::OnPrint(CDC * pDC, CPrintInfo * pInfo)
{
// 	double dImgWidth = 800.0;
// 	double dImgHeight = 480.0;

	double dImgWidth = 1280;
	double dImgHeight = 720;

	//////////////////////
	//by qzg 2019.10.21
	int nIdx = g_GJData.GetSelGJId();
	CString strYiQiType = g_GJData.GetYiQiType(nIdx);

//	int		nImgWid = nImgWidth;
	if (strYiQiType == strYiQi_U8_1024_768)
	{
		dImgWidth = 1024;
		dImgHeight = 768;
	}
	else if (strYiQiType == strYiQi_U9_1280_800)
	{
		dImgWidth = 1280;
		dImgHeight = 800;
	}
	else if (strYiQiType == strYiQi_CK103_1280_720)
	{
		dImgWidth = 1280;
		dImgHeight = 720;
	}
	////////////////////

	double dShow = 10 / 11.0;

	int nWidth = pDC->GetDeviceCaps(HORZRES);
	int nHeight = pDC->GetDeviceCaps(VERTRES);

	int nSizeX = pDC->GetDeviceCaps(HORZSIZE);
	int nSizeY = pDC->GetDeviceCaps(VERTSIZE);

	int nBoderTop = (int)(nHeight * g_printSetting.m_dMarginTop * 10.0 / nSizeY);
	int nBoderBottom = (int)(nHeight * g_printSetting.m_dMarginBottom * 10.0 / nSizeY);
	int nBoderLeft = (int)(nWidth * g_printSetting.m_dMarginLeft * 10.0 / nSizeX);
	int nBoderRight = (int)(nWidth * g_printSetting.m_dMarginRight * 10.0 / nSizeX);

	int nDrawWidth = (nWidth - nBoderLeft - nBoderRight) / g_printSetting.m_nCols;
	int nDrawHeight = (nHeight - nBoderTop - nBoderBottom) / g_printSetting.m_nRows;

	int nTemp = (int)(nDrawWidth / dImgWidth * dImgHeight);
	SIZE szImg;
	if (nTemp > nDrawHeight)			// 计算绘图区域尺寸
	{
		szImg.cx = (LONG)(nDrawHeight / dImgHeight * dImgWidth * dShow);
		szImg.cy = (LONG)(nDrawHeight * dShow);
	}
	else
	{
		szImg.cx = (LONG)(nDrawWidth * dShow);
		szImg.cy = (LONG)(nDrawWidth / dImgWidth * dImgHeight * dShow);
	}

	//CArray<int, int> arrGJSel;
	//arrGJSel.RemoveAll();

	//for (int i = 0; i < m_listGJ.GetItemCount(); i++)
	//{
	//	if (m_listGJ.GetCheck(i))
	//	{
	//		arrGJSel.Add(i);
	//	}
	//}
	//if (arrGJSel.GetSize() <= 0)
	//{
	//	arrGJSel.Add(g_GJData.GetSelGJId());
	//}

	int nPages = (m_ArrGJSel.GetSize() - 1) / (g_printSetting.m_nCols * g_printSetting.m_nRows) + 1;

	CImgOperator img;
	CRect rtImg;
	POINT ptImg;
	ptImg.x = 0;
	ptImg.y = 0;
	for (int nPage = 0; nPage < nPages; nPage++)
	{
		if (nPage + 1 == pInfo->m_nCurPage)
		{
			// 绘制页眉和页脚，以及页码
			CFont font, *pOldFont;
			CSize szTxt;
			CString strText;
			font.CreatePointFont(800, L"仿宋");

			pOldFont = pDC->SelectObject(&font);

			if (g_printSetting.m_bPrintHeader)
			{
				strText = g_printSetting.m_strHeader;
				szTxt = pDC->GetTextExtent(strText);
				pDC->TextOut((nWidth - szTxt.cx) / 2, (nBoderTop - szTxt.cy) / 2, strText);
			}

			if (g_printSetting.m_bPrintFooter)
			{
				strText = g_printSetting.m_strFooter;
				szTxt = pDC->GetTextExtent(strText);
				pDC->TextOut((nWidth - szTxt.cx) / 2, (nBoderBottom - szTxt.cy) / 2 + (nHeight - nBoderBottom), strText);
			}

			if (g_printSetting.m_bPrintPage)
			{
				strText.Format(L"%d", nPage + g_printSetting.m_nStartPage),
					szTxt = pDC->GetTextExtent(strText);
				pDC->TextOut(nWidth - szTxt.cx - nBoderRight, (nBoderBottom - szTxt.cy) / 2 + (nHeight - nBoderBottom), strText);
			}

			pDC->SelectObject(pOldFont);
			font.DeleteObject();

			for (int i = 0; i < g_printSetting.m_nRows; i++)		// 绘制图片信息
			{
				for (int j = 0; j < g_printSetting.m_nCols; j++)
				{
					int nIdx = i * g_printSetting.m_nCols + j + nPage * g_printSetting.m_nCols * g_printSetting.m_nRows;
					if (nIdx < m_ArrGJSel.GetSize())
					{
						rtImg.left = nBoderLeft + j * nDrawWidth + (nDrawWidth - szImg.cx) / 2;
						rtImg.right = rtImg.left + szImg.cx;
						rtImg.top = nBoderTop + i * nDrawHeight + (nDrawHeight - szImg.cy) / 2;
						rtImg.bottom = rtImg.top + szImg.cy;

						img.SetImgInfo(m_ArrGJSel[nIdx]);
						img.DrawCrackInfo(DRAW_ALL, 0, false);
						img.ShowImage(pDC, rtImg, ptImg, szImg.cx / dImgWidth);
					}
				}
			}
		}
	}
}

void CPrintSetDlg::LoadSetting()
{
	SetEdit(&m_editMarginTop, g_printSetting.m_dMarginTop);
	SetEdit(&m_editMarginBottom, g_printSetting.m_dMarginBottom);
	SetEdit(&m_editMarginLeft, g_printSetting.m_dMarginLeft);
	SetEdit(&m_editMarginRight, g_printSetting.m_dMarginRight);

	m_btnPrintPage.SetCheck(g_printSetting.m_bPrintPage);
	m_nStartPage = g_printSetting.m_nStartPage;
	GetDlgItem(IDC_EDIT_START_PAGE)->EnableWindow(g_printSetting.m_bPrintPage);

	m_btnPrintHeader.SetCheck(g_printSetting.m_bPrintHeader);
	m_strHeader = g_printSetting.m_strHeader;
	GetDlgItem(IDC_EDIT_HEADER)->EnableWindow(g_printSetting.m_bPrintHeader);

	m_btnPrintFooter.SetCheck(g_printSetting.m_bPrintFooter);
	m_strFooter = g_printSetting.m_strFooter;
	GetDlgItem(IDC_EDIT_FOOTER)->EnableWindow(g_printSetting.m_bPrintFooter);

	m_nPrintCol = g_printSetting.m_nCols;
	m_nPrintRow = g_printSetting.m_nRows;

	UpdateData(FALSE);
}

BOOL CPrintSetDlg::OnInitDialog()
{
	CMyDialog::OnInitDialog();

	m_spinRow.SetRange(nRowMin, nRowMax);
	m_spinCol.SetRange(nColMin, nRowMax);

	LoadSetting();

	this->SetDlgBkColor(RGB(51, 51, 51), RGB(255, 255, 255));
	this->SetDlgBorderColor(RGB(51, 51, 51), 2);
	this->SetStaticColor(RGB(0, 0, 0), RGB(255, 255, 255));

	//设置对话框上控件的语言文字
	g_SetDialogStrings(this, IDD);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CPrintSetDlg::SetEdit(CNumEdit * edit, const double& dValue)
{
	edit->SetDotLeftNum(nDotLeftNum);
	edit->SetDotRightNum(nDotRightNum);
	edit->SetMaxValue(dMarginMax);
	edit->SetMinValue(dMarginMin);

	CString strValue, strFormat;
	strFormat.Format(L"%%.%df", nDotRightNum);
	strValue.Format(strFormat, dValue);
	edit->SetWindowText(strValue);
}

void CPrintSetDlg::SaveSetting()
{
	USES_CONVERSION;

	UpdateData(TRUE);

	g_printSetting.m_nRows = m_nPrintRow;
	g_printSetting.m_nCols = m_nPrintCol;

	CString strValue;
	m_editMarginTop.GetWindowText(strValue);
	g_printSetting.m_dMarginTop = wcstod(strValue.GetBuffer(), NULL);

	m_editMarginBottom.GetWindowText(strValue);
	g_printSetting.m_dMarginBottom = wcstod(strValue.GetBuffer(), NULL);

	m_editMarginLeft.GetWindowText(strValue);
	g_printSetting.m_dMarginLeft = wcstod(strValue.GetBuffer(), NULL);

	m_editMarginRight.GetWindowText(strValue);
	g_printSetting.m_dMarginRight = wcstod(strValue.GetBuffer(), NULL);

	g_printSetting.m_bPrintPage = m_btnPrintPage.GetCheck();
	g_printSetting.m_nStartPage = m_nStartPage;

	g_printSetting.m_bPrintHeader = m_btnPrintHeader.GetCheck();
	g_printSetting.m_bPrintFooter = m_btnPrintFooter.GetCheck();

	strcpy_s(g_printSetting.m_strHeader, sizeof(g_printSetting.m_strHeader), W2A(m_strHeader));
	strcpy_s(g_printSetting.m_strFooter, sizeof(g_printSetting.m_strFooter), W2A(m_strFooter));
}

void CPrintSetDlg::PrintImg()
{
	int nGJSel = m_ArrGJSel.GetSize();

	g_nPageCnt = (nGJSel - 1) / (g_printSetting.m_nCols * g_printSetting.m_nRows) + 1;
	switch (m_nPrint)
	{
	case 0:
		m_PreFrame = new CMainFrame(Drawf, this, this, true);	//第4个参数为true表示直接打印
		break;
	case 1:
		m_PreFrame = new CMainFrame(Drawf, this, this, false);	//第4个参数为true表示直接打印
		m_PreFrame->ShowWindow(SW_SHOWMAXIMIZED);
		m_PreFrame->UpdateWindow();
		break;
	default:
		m_PreFrame = new CMainFrame(Drawf, this, this, false);	//第4个参数为true表示直接打印
		m_PreFrame->ShowWindow(SW_SHOWMAXIMIZED);
		m_PreFrame->UpdateWindow();
		break;
	}
}

void CPrintSetDlg::OnBnClickedCheckPrintPage()
{
	GetDlgItem(IDC_EDIT_START_PAGE)->EnableWindow(m_btnPrintPage.GetCheck());
}


void CPrintSetDlg::OnBnClickedCheckPrintHeader()
{
	GetDlgItem(IDC_EDIT_HEADER)->EnableWindow(m_btnPrintHeader.GetCheck());
}


void CPrintSetDlg::OnBnClickedCheckPrintFooter()
{
	GetDlgItem(IDC_EDIT_FOOTER)->EnableWindow(m_btnPrintFooter.GetCheck());
}

void CPrintSetDlg::GetNextValue(CEdit* edit, const int & nFlag)
{
	CString strMargin;
	edit->GetWindowText(strMargin);

	
	double dValue = wcstod(strMargin.GetBuffer(), NULL);
	double dResult;

	dResult = dValue - nFlag / (((CNumEdit*)edit)->GetDotRightNum() * 10.0);

	if (dResult < dMarginMin)
	{
		dResult = dMarginMin;
	}
	if (dResult > dMarginMax)
	{
		dResult = dMarginMax;
	}

	if (dResult != dValue)
	{
		CString strFormat;
		strFormat.Format(L"%%.%df", ((CNumEdit*)edit)->GetDotRightNum());
		strMargin.Format(strFormat, dResult);

		edit->SetWindowText(strMargin);
	}
	
	return;
}

void CPrintSetDlg::OnDeltaposSpinMarginTop(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	
	GetNextValue(&m_editMarginTop, pNMUpDown->iDelta);

	*pResult = 0;
}


void CPrintSetDlg::OnDeltaposSpinMarginBottom(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	
	GetNextValue(&m_editMarginBottom, pNMUpDown->iDelta);

	*pResult = 0;
}


void CPrintSetDlg::OnDeltaposSpinMarginLeft(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	
	GetNextValue(&m_editMarginLeft, pNMUpDown->iDelta);

	*pResult = 0;
}


void CPrintSetDlg::OnDeltaposSpinMarginRight(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	GetNextValue(&m_editMarginRight, pNMUpDown->iDelta);

	*pResult = 0;
}


void CPrintSetDlg::OnBnClickedOk()
{
	//SaveSetting();
	//CMyDialog::OnOK();
}


void CPrintSetDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CMyDialog::OnCancel();
}


void CPrintSetDlg::OnBnClickedButtonPrintView()
{
	m_nPrint = 1;
	SaveSetting();
	PrintImg();
	//CMyDialog::OnOK();
}


void CPrintSetDlg::OnBnClickedButtonPrint()
{
	m_nPrint = 0;
	SaveSetting();
	PrintImg();
	//CMyDialog::OnOK();
}


void CPrintSetDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//CMainFrame* pf = (CMainFrame*)::AfxGetMainWnd();
	CWinApp *pApp = AfxGetApp();
	pApp->m_pMainWnd = GetParent();						// 避免程序异常退出。
	//pf->DestroyWindow();
	SaveSetting();
	CMyDialog::OnClose();
}


void CPrintSetDlg::OnBnClickedButtonPrintDefault()
{
	g_printSetting.Init();
	LoadSetting();
}
